#include "../logs/maglog.hpp"
#include "bench.h"

namespace maglog {

// 通用日志测试函数：用于执行日志系统的压力测试
// 参数：
// - type: 日志类型（同步或异步）。
// - thread_count: 线程数量。
// - msg_count: 日志数量。
// - msglen: 单条日志的长度。
void bench_thread_log(const std::string &type, size_t thread_count, size_t msg_count, size_t msglen)
{
    static int num = 1;
    std::string logger_name = type + "_bench_logger" + std::to_string(num++);
    
    LOGI("************************************************");
    LOGI("%s日志测试: %d 线程, %d 条日志", type.c_str(), thread_count, msg_count);

    // 构建日志器
    maglog::GlobalLoggerBuilder::ptr lbp(new maglog::GlobalLoggerBuilder);
    lbp->buildLoggerName(logger_name);
    lbp->buildFormatter("%m");
    lbp->buildSink<maglog::FileSink>("./logs/" + type + ".log");
    lbp->buildLoggerType(type == "sync" ? maglog::Logger::Type::LOGGER_SYNC : maglog::Logger::Type::LOGGER_ASYNC);
    lbp->build(); 

    // 运行压力测试
    maglog::bench(logger_name, thread_count, msglen, msg_count);
    LOGI("************************************************");
}

// 压力测试函数：分别测试异步和同步日志
void bench_test() {
    /* 异步日志测试 */
    bench_thread_log("async", 1, 1000000, 100); // 单线程异步日志测试
    bench_thread_log("async", 5, 1000000, 100); // 多线程异步日志测试

    /* 同步日志测试 */
    bench_thread_log("sync", 1, 1000000, 100); // 单线程同步日志测试
    bench_thread_log("sync", 5, 1000000, 100); // 多线程同步日志测试
}

} // namespace maglog

int main(int argc, char *argv[])
{
    maglog::bench_test(); // 运行压力测试

    return 0;
}





// #include "../logs/bitlog.h"
// #include "bench.h"

// void sync_bench_thread_log(size_t thread_count, size_t msg_count, size_t msglen)
// {
//     static int num = 1;
//     std::string logger_name = "sync_bench_logger" + std::to_string(num++);
//     LOGI("************************************************");
//     LOGI("同步日志测试: %d threads, %d messages", thread_count, msg_count);

//     bitlog::GlobalLoggerBuilder::ptr lbp(new bitlog::GlobalLoggerBuilder);
//     lbp->buildLoggerName(logger_name);
//     lbp->buildFormatter("%m");
//     lbp->buildSink<bitlog::FileSink>("./logs/sync.log");
//     lbp->buildLoggerType(bitlog::Logger::Type::LOGGER_SYNC);
//     lbp->build(); 
//     bitlog::bench(logger_name, thread_count, msglen, msg_count);
//     LOGI("************************************************");
// }
// void async_bench_thread_log(size_t thread_count, size_t msg_count, size_t msglen)
// {
//     static int num = 1;
//     std::string logger_name = "async_bench_logger" + std::to_string(num++);
//     LOGI("************************************************");
//     LOGI("异步日志测试: %d threads, %d messages", thread_count, msg_count);

//     bitlog::GlobalLoggerBuilder::ptr lbp(new bitlog::GlobalLoggerBuilder);
//     lbp->buildLoggerName(logger_name);
//     lbp->buildFormatter("%m");
//     lbp->buildSink<bitlog::FileSink>("./logs/async.log");
//     lbp->buildLoggerType(bitlog::Logger::Type::LOGGER_ASYNC);
//     lbp->build(); 
//     bitlog::bench(logger_name, thread_count, msglen, msg_count);
//     LOGI("************************************************");
// }
// void bench_test() {
//     /*异步日志输出*/
//     async_bench_thread_log(1, 1000000, 100);
//     async_bench_thread_log(5, 1000000, 100);
//     // 同步写日志
//     sync_bench_thread_log(1, 1000000, 100);
//     sync_bench_thread_log(5, 1000000, 100);
// }

// int main(int argc, char *argv[])
// {
//     bench_test();

//     return 0;
// }
